﻿Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.UI.Styles

Namespace Global.Runa.UI.Controls

    Public Class TypeTextLine
        Inherits TextLine

        Protected _Texts As Char()
        Protected _TextsLeft As Single()

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New(Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase, ByVal textStyle As TextStyle)
            MyBase.New(Name, Parent, textStyle)
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase, ByVal [Text] As String, ByVal textStyle As TextStyle)
            MyBase.New(Name, Parent, [Text], textStyle)
        End Sub

        Private Function GetDrawX() As Single
            Dim result As Single = 0.0!
            If (Me._TextsLeft Is Nothing) Then
                Return 0.0!
            End If
            If (MyBase._Progress <= Me._TextsLeft(1)) Then
                Return 0.0!
            End If
            Dim i As Integer
            For i = 1 To Me._TextsLeft.Length - 1
                If (MyBase._Progress > Me._TextsLeft((i - 1))) Then
                    result = Me._TextsLeft(i)
                End If
            Next i
            Return result
        End Function

        Public Overrides Property Progress As Single
            Get
                Return MyBase.Progress
            End Get
            Set(ByVal value As Single)
                If (((MyBase._Progress <> value) AndAlso (value >= 0.0!)) AndAlso (value <= 1.0!)) Then
                    MyBase._Progress = value
                    If ((Not MyBase.Content Is Nothing) AndAlso (Not MyBase.Content.TextTexture Is Nothing)) Then
                        Dim x As Single = Me.GetDrawX
                        MyBase.DrawClip = New Rectangle(Me.DrawBounds.X, Me.DrawBounds.Y, CInt((Me.DrawBounds.Width * x)), Me.DrawBounds.Height)
                        MyBase.TextClip = New Rectangle(0, 0, CInt((MyBase.Content.TextTexture.Texture.Width * x)), MyBase.Content.TextTexture.Texture.Height)
                    End If
                End If
            End Set
        End Property

        Public Overrides Property [Text] As String
            Get
                Return MyBase.Text
            End Get
            Set(ByVal value As String)
                If (MyBase._Text <> value) Then
                    MyBase._Text = value
                    MyBase.Content = New TextContainerBase(MyBase.Game, value, Me.Size, MyBase._TextStyle)
                    Dim width As Single = Me._Size.X
                    Me._Texts = value.ToCharArray
                    Me._TextsLeft = New Single(Me._Texts.Length - 1) {}
                    Dim str As String = ""
                    Dim i As Integer
                    For i = 0 To value.Length - 1
                        Me._TextsLeft(i) = (MyBase.Content.Font.MeasureString(str).X / width)
                        str = (str & Me._Texts(i))
                    Next i
                    If (Me.Progress = 0.0!) Then
                        Me.Progress = 1.0!
                    End If
                End If
            End Set
        End Property

    End Class

End Namespace